package com.kaisademo.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaisademo.zhxy.pojo.Grade;
import com.kaisademo.zhxy.service.GradeService;
import com.kaisademo.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @ApiOperation("获取所有Grade信息")
    @GetMapping("/getGrades")
    public Result getGrades(){
        List<Grade> gradeList = gradeService.getGrades();
        return Result.ok(gradeList);
    }
    /**
     * 删除年级
     * @param ids 根据id删除
     * @return 成功/失败
     */
    @ApiOperation("删除一个或者多个grade信息")
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(
            @ApiParam("JSON的年级id集合,映射为后台List<Integer>")
            @RequestBody List<Integer> ids){
        gradeService.removeByIds(ids);
        return Result.ok();
    }
    /**
     * 新增或修改
     * @param grade 年级对象
     * @return 成功/失败
     */
    @ApiOperation("添加或者修改年级信息")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(
            @ApiParam("JSON的grade对象转换后台数据模型")
            @RequestBody Grade grade){
        gradeService.saveOrUpdate(grade);
        return Result.ok();
    }

    /**
     * 分页条件查询
     * @param pageNo 页码数
     * @param pageSize 页大小
     * @param gradeName 模糊查询条件
     * @return 分页查询结果
     */
    @ApiOperation("查询年级信息,分页带条件")
    @GetMapping("/getGrade/{pageNo}/{pageSize}")
    public Result getGradeByOpr(
            @ApiParam("分页查询页码数")
            @PathVariable(value = "pageNo") Integer pageNo,
            @ApiParam("分页查询页大小")
            @PathVariable(value = "pageSize") Integer pageSize,
            @ApiParam("分页查询模糊匹配班级名")
                    String gradeName

    ){
        //分页构造器
        Page<Grade> gradePage = new Page<>(pageNo,pageSize);
        //调用服务层方法,传入分页信息,和查询的条件
        IPage<Grade> gradeIPage = gradeService.getGradeByOpr(gradePage,gradeName);
        return Result.ok(gradeIPage);
    }
}
